package edu.qd.learn.day05;

import java.util.Arrays;

/**
 * 学习一下栈
 * <p>
 * : 先进后出  后进先出
 * <p>
 * 方法:
 * 入栈  压栈
 * 出栈  弹栈
 * 实现这个栈
 *
 * @author 布衣
 */
public class LearnStack01 {

    //底层结构
    private int[] stack;
    private int[] minStack;
    //默认长度
    private final int DEFAULT_CAPACITY = 10;

    //现有元素个数
    private int size;

    /**
     * 面向对象的实例化方法 构造方法
     */
    public LearnStack01() {
        //就是不想让我初始化的
    }

    public LearnStack01(int capacity) {
        //想初始化的，按照 你给的capacity来初始化
        stack = new int[capacity];
        minStack = new int[capacity+1];
    }

    public boolean push(int x1) {

        if (stack == null) {
            stack = new int[DEFAULT_CAPACITY];
            minStack = new int[DEFAULT_CAPACITY];
            minStack[0] = Integer.MAX_VALUE;
        }

        if (size == stack.length) {
            stack = Arrays.copyOf(stack, stack.length * 2);
            minStack = Arrays.copyOf(minStack, minStack.length * 2);
        }

        // 原数
        stack[size++] = x1;

        //最小栈
        minStack[size] = Math.min(minStack[size-1], x1);

        return true;
    }

    public void pop() {
        if (size == 0) {
            throw new RuntimeException("栈为空");
        }
        --size;
    }

    public int top() {
        if (size == 0) {
            throw new RuntimeException("栈为空");
        }
        return stack[size - 1];
    }

    public int size() {
        return size;
    }

    public boolean isEmpty() {
        return size == 0;
    }

    public int getMin(){
        return minStack[size];
//        return Arrays.stream(stack).min().getAsInt();
//        int min = 0;
//        for(int i = 0 ;i < size;i++){
//            if(min > stack[i]){
//                min = stack[i];
//            }
//        }
//        return min;
    }

    public static void main(String[] args) {
        LearnStack01 l = new LearnStack01();
        l.push(-2);
        l.push(1);
        l.push(-3);
        System.out.println(l.getMin());
        l.pop();
        System.out.println(l.getMin());


    }


}
